package com.cheyun.netsalev3.viewmodel

import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Transformations
import com.cheyun.netsalev3.bean.DataX
import com.cheyun.netsalev3.repository.CallRecordRepository
import com.cheyun.netsalev3.widget.CallPopFragment

class CallRecordActivityViewModel(var callRecordRepository: CallRecordRepository) : BaseViewModel() {


    var listdata = callRecordRepository.loadAndroidData()
    var networkState = Transformations.switchMap(listdata) {
        callRecordRepository.networkState
    }!!

    /**
     * 刷新
     */
    fun refresh() {
        callRecordRepository.dataSourceFactory.sourceLiveData.value?.invalidate()
    }

    /**
     * 点击播放
     */
    fun onClicPop(view: View, item: DataX) {
        if (!item.record.contains("http")) {
            Toast.makeText(view.context, "无法播放", Toast.LENGTH_LONG).show()
            return
        }

        var context = view.context
        if (context is AppCompatActivity) {
            var pop = CallPopFragment.newInstance(item.record,item.starttime,item.timesName)
            pop.show(context.supportFragmentManager, "")
        }
    }
}